//pjax

function drawHTML(data, state){
	if(state){ // not pop
		history.pushState(data, data.title, data.url);
	}
	var content =data.content;
	$(".container").fadeOut('fast', function(){
		$(".container").html(content)
		.fadeIn('fast');
	});
}

function loadPage(){
	var targeturl = this.href;
	if(this.host == window.location.host){
		$.ajax({
			url: targeturl,
			type: 'POST',
			async: false,
			success: function(data, state, jqXHR){
				if(jqXHR.getResponseHeader('content-type') == "application/json"){
					drawHTML(data, true);
				}else{
					location.href=targeturl;
				}
			},
			error: function(){
				location.href=targeturl;
			},
		});
	}else{
		location.href=targeturl;
	}
}

function init(){
	$("body").delegate('a', 'click', function(){return false;});
	$("body").delegate('a:not(.button)', 'click', loadPage);
}
window.addEventListener('load', init, false);
